package com.seems.janggu;

import java.util.List;

import com.seems.janggu.HitInfo.HitItem;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.os.Handler;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

public class ScoreView extends View {

	private static final String TAG = "ScoreView";
	private static final int VIEW_TIME_WIDTH = 4000;
	private static final int BAR_WIDTH = 5;
	private static final int BAR_MAX_HEIGHT = 100;
	private static final int BAR_COUNT = 12;
	private static final int FREQUENCY = 700;
	private Paint mPaint;
	Handler mHandler;

	private long mDrawMilliSec;
	private HitInfo mHitInfo;
	private int mBukFrequency = 700;

	public ScoreView(Context context) {
		super(context);
		init();
		// TODO Auto-generated constructor stub
	}

	public ScoreView(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
	}

	public ScoreView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init();
	}

	private void init() {
		mPaint = new Paint();
		mHitInfo = HitInfo.getInstance();
	}

	private int mapMilliSecToXPosition(long eventMilliSec) {
		return getMeasuredWidth()
				* (int) (eventMilliSec - mDrawMilliSec + VIEW_TIME_WIDTH/2)/VIEW_TIME_WIDTH;
	}

	private void drawHitItem(Canvas canvas, HitItem item) {
		int position = mapMilliSecToXPosition(item.getMilliSec());
		mPaint.setColor(getResources().getColor(R.color.hit_bar));

		switch (item.getHitType()) {
		case HitItem.HIT_TYPE_DUNG:
			canvas.drawRect(position, getMeasuredHeight() / 2 - (getMeasuredHeight() / 2)
					* item.getPressure(), position + BAR_WIDTH,
					getMeasuredHeight() / 2, mPaint);
			canvas.drawRect(position, getMeasuredHeight() / 2, position
					+ BAR_WIDTH, getMeasuredHeight() / 2 + (getMeasuredHeight() / 2)
					* item.getPressure(), mPaint);
			break;
		case HitItem.HIT_TYPE_GI:
		case HitItem.HIT_TYPE_DDA:
			canvas.drawRect(position, getMeasuredHeight() / 2, position
					+ BAR_WIDTH, getMeasuredHeight() / 2 + (getMeasuredHeight() / 2)
					* item.getPressure(), mPaint);
			break;
		case HitItem.HIT_TYPE_GUNG:
			canvas.drawRect(position, getMeasuredHeight() / 2 - (getMeasuredHeight() / 2)
					* item.getPressure(), position + BAR_WIDTH,
					getMeasuredHeight() / 2, mPaint);
			break;
		default:
			break;
		}
	}

	private void drawFrame(Canvas canvas){
		int i;
		int width = 2;
		int drawStart = (int) (mBukFrequency - mDrawMilliSec % mBukFrequency);
		mPaint.setColor(getResources().getColor(R.color.frame));
		
		for(i = 0; i < VIEW_TIME_WIDTH/mBukFrequency; i++){
			int x = (int)((drawStart + mBukFrequency * i) / (float)VIEW_TIME_WIDTH * getMeasuredWidth());
			//Log.d(TAG, "x : " + x);
			canvas.drawRect(x, 0, x + width, getMeasuredHeight(), mPaint);
		}
		mPaint.setColor(getResources().getColor(R.color.metronome));
		canvas.drawRect(getMeasuredWidth()/2, 0, getMeasuredWidth()/2+4, getMeasuredHeight(), mPaint);
		canvas.drawRect(0, getMeasuredHeight()/2, getMeasuredWidth(), getMeasuredHeight()/2+2, mPaint);
	}
	@Override
	protected void onDraw(Canvas canvas) {
		List<HitItem> hitList;
		long fromMilliSec, toMilliSec;

		fromMilliSec = mDrawMilliSec - VIEW_TIME_WIDTH / 2;
		toMilliSec = mDrawMilliSec + VIEW_TIME_WIDTH / 2;
		hitList = mHitInfo.getHitList(fromMilliSec, toMilliSec);
		if (hitList != null) {
			for (int i = 0; i < hitList.size(); i++) {
				drawHitItem(canvas, hitList.get(i));
			}
		}
		
		drawFrame(canvas);

		super.onDraw(canvas);
	}

	public void setBukFrequency(int frequency){
		mBukFrequency = frequency;
	}
	
	public void updateScoreView(long milliSec) {
		mDrawMilliSec = milliSec;
		//Log.d(TAG, "draw Time : " + milliSec);
		invalidate();
	}

}
